package cn.pugle.oj.leetcode;

import cn.pugle.oj.catalog.LinkedProblem;
import cn.pugle.oj.catalog.Unknown;

/**
 * TODO 每天一遍, 防止抑郁
 * TODO 再写一遍飞哥说的, 每k个一倒转
 *
 * @author tzp
 * @since 2020/11/3
 */
public class LC24 implements LinkedProblem {
    public ListNode swapPairs1(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode newHead = head.next;
        head.next = swapPairs(head.next.next);
        newHead.next = head;
        return newHead;
    }

    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode dummy = new ListNode(), pre = dummy, cur = head;
        while (cur != null && cur.next != null) {
            pre.next = cur.next;
            cur.next = cur.next.next;
            pre.next.next = cur;

            pre = cur;
            cur = cur.next;
        }
        return dummy.next;
    }

    public static void main(String[] args) {
        System.out.println(new LC24().swapPairs(ListNode.arrayToListNode(new int[]{1, 2})));
        System.out.println(new LC24().swapPairs(ListNode.arrayToListNode(new int[]{1, 2, 3})));
        System.out.println(new LC24().swapPairs(ListNode.arrayToListNode(new int[]{1, 2, 3, 4, 5})));
    }
}
